package dbb

import (
	"bytes"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"github.com/y2h/fyne_poem/widgets"
)

func toFile() widget.ToolbarItem {
	return widget.NewToolbarAction(theme.ContentPasteIcon(), func() {
		w := widgets.EmptyWin("New database")
		fp := "bak.sql"
		bs := binding.BindString(&fp)
		cc := widget.NewEntryWithData(bs)
		txt := widget.NewMultiLineEntry()
		buf := bytes.NewBufferString("")
		btn := widget.NewButton("Dump To Entry", func() {
			buf.Reset()
			eng := gf2xorm(Me.Db)
			// tables, _ := eng.DBMetas()
			// eng.DumpTables(tables, buf)
			eng.DumpAll(buf)

			txt.SetText(buf.String())
		})
		btn1 := widget.NewButton("Dump To File", func() {
			buf.Reset()
			eng := gf2xorm(Me.Db)
			// tables, _ := eng.DBMetas()
			// eng.DumpTables(tables, buf)
			eng.DumpAll(buf)
			eng.DumpAllToFile(fp)

			txt.SetText("Dump below to "+fp+"\n\n"+ buf.String())
		})
		top := container.NewVBox(cc,
			container.NewHBox(btn, btn1))
		w.SetContent(container.NewBorder(top, nil, nil, nil, txt))
		w.Show()
	})
}
